import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  NProgress.start()

  const token = store.state.user.token
  if (token) {
    await store.dispatch('dashboard/getDashBoardInfo')
    if (to.path === '/login') {
      next('/')
      NProgress.done()
      next(to)
    } else {
      // if (!store.state.user.id) { await store.dispatch('user/logout') }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  next()
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
